package com.bonana.backend.controller;
import com.alibaba.fastjson.JSON;
import com.bonana.core.entity.CoreUser;
import com.bonana.core.entity.mybatis.Boboplan;
import com.bonana.core.entity.mybatis.IndexBo;
import com.bonana.core.entity.mybatis.Platform;
import com.bonana.core.form.BoPlanFrom;
import com.bonana.core.form.UserForm;
import com.bonana.core.result.Response;
import com.bonana.core.result.ResponseCode;
import com.bonana.core.services.ProjectService;
import com.bonana.core.services.UtilsService;
import com.bonana.core.vo.UserCardVO;
import com.bonana.core.vo.UserProfileVO;
import com.bonana.core.entity.CoreUserProfile;
import com.bonana.core.services.BoBoService;
import com.bonana.utils.ConvertUtils;
import com.bonana.utils.TimestampUtil;
import com.github.pagehelper.PageInfo;
import com.google.common.net.InetAddresses;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2016/6/27.
 */
@Controller
@RequestMapping(value="/bobo")
public class BoBoManagerController {
    @Autowired
    BoBoService boBoService;

    @Autowired
    UtilsService utilsService;

    @Autowired
    private HttpSession session;


    private int getCurUserType() {
        int utype = 0;
        if (session.getAttribute("session.usertype") != null) {
            utype = ConvertUtils.toInteger(session.getAttribute("session.usertype"));
        }
        return utype;
    }
    private int getCurUid() {
        int uid = 0;
        if (session.getAttribute("session.uid") != null) {
            uid = ConvertUtils.toInteger(session.getAttribute("session.uid"));
        }
        return uid;
    }

    @RequestMapping(value="getBoboer", method = RequestMethod.POST)
    @ResponseBody
    public PageInfo getBoboer(Integer pgNum, Integer pgSize,
                            @RequestParam(required = false) String query,
                            @RequestParam(required = false) Integer platformId) {
        List<CoreUserProfile> list = null;
        if (getCurUserType() == 1) {
            list = boBoService.getBobos(pgNum, pgSize,null, query, null , platformId );
        } else {
            list = boBoService.getBobos(pgNum, pgSize,null, query, getCurUid(), platformId);
        }
        return new PageInfo(list);
    }

    @RequestMapping(value="/manager")
    public String init(Model model) {
        List<Platform> platforms = utilsService.getPlatform();
        model.addAttribute("platforms", platforms);
        return "user/list";
    }

    @RequestMapping(value="/calendar")
    public String calendarMrg(@RequestParam(required = false) Integer boId, Model model) {
        List<Platform> platforms = utilsService.getPlatform();
        model.addAttribute("platforms", platforms);
        CoreUserProfile profile = boBoService.queryUserProfileByUid(boId, (long) 0);
        model.addAttribute("boId", boId);
        model.addAttribute("boName", profile.getNickname());
        List<Boboplan> boplans = boBoService.getBoboPlan(boId);
        model.addAttribute("boplans", boplans);
        return "user/calendar";
    }

    @RequestMapping("save")
    @ResponseBody
    public UserProfileVO saveUserProfile(UserForm userForm, HttpServletRequest request)  {
        if (userForm.getUid() == 0) {
            CoreUser coreUser = new CoreUser();
            coreUser.setUsername(userForm.getUsername());
            coreUser.setCreatedTime(TimestampUtil.now());
            coreUser.setUpdatedTime(TimestampUtil.now());
            coreUser.setLastLoginTime(TimestampUtil.now());
            coreUser.setIsRobot((byte) 1);
            coreUser.setStatus((byte) 0);
            coreUser.setCredits(0);
            if (getCurUserType() == 1) {
                coreUser.setPartnerId(0);
            } else {
                coreUser.setPartnerId(getCurUid());
            }
            coreUser.setLevelCredits(0);
            coreUser.setPassword(DigestUtils.md5Hex(userForm.getUsername() + "111111"));
            userForm.setPassword("111111");
            coreUser.setLastLoginIp(InetAddresses.coerceToInteger(InetAddresses.forString(request.getRemoteAddr())));
            boBoService.AddBobo(coreUser);
        }
        return boBoService.saveUser(userForm, request.getRemoteAddr());
    }

    @RequestMapping("removeFromIndex")
    @ResponseBody
    public Response removeFromIndex(Integer uid) {
        if (1==boBoService.RemoveFromIndex(uid)) {
            return new Response(ResponseCode.SUCC);
        } else {
            return new Response(ResponseCode.ITEM_NOT_FOUND);
        }
    }

    @RequestMapping("delete")
    @ResponseBody
    public Response delete(Integer uid) {
        if(1==boBoService.DelBoboById(uid)) {
            return new Response(ResponseCode.SUCC);
        } else {
            return new Response(ResponseCode.ITEM_NOT_FOUND);
        }
    }

    @RequestMapping("AddToIndex")
    @ResponseBody
    public Response AddToIndex(IndexBo uid) {
        try {
            if (1 == boBoService.AddToIndex(uid)) {
                return new Response(ResponseCode.SUCC);
            } else {
                return new Response(ResponseCode.ITEM_NOT_FOUND);
            }
        } catch (Exception e) {
            return new Response(ResponseCode.ALREADY_EXIST);
        }
    }

    @RequestMapping("AddBoboPlan")
    @ResponseBody
    public Boboplan AddBoboPlan(BoPlanFrom plan) {
        return boBoService.SaveBoboPlan(plan);
    }

    @RequestMapping("AddBoboPlanForm")
    @ResponseBody
    public Response AddBoboPlanForm(BoPlanFrom plan) {
        if (boBoService.SaveBoboPlan(plan).getPlanId()>0) {
            return new Response(ResponseCode.SUCC);
        } else {
            return new Response(ResponseCode.ERROR);
        }
    }

    @RequestMapping("DelBoboPlan")
    @ResponseBody
    public Response AddBoboPlan(Integer planId) {
        if(1==boBoService.RemovePlan(planId)) {
            return new Response(ResponseCode.SUCC);
        } else {
            return new Response(ResponseCode.ITEM_NOT_FOUND);
        }
    }

    @RequestMapping("SaveBoboPlan")
    @ResponseBody
    public Boboplan SaveBoboPlan(BoPlanFrom plan) {
        return boBoService.SaveBoboPlan(plan);
    }
}
